package com.keta.foundation.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.DictDAO;
import com.keta.foundation.entity.Dict;
import com.keta.foundation.entity.Module;
import com.keta.foundation.exception.ExistedException;
import com.keta.foundation.service.DictService;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;



@Service
@Transactional
public class DictServiceImpl implements DictService {

	@Autowired
	private DictDAO dictDAO;
	
	@Override
	public List<Dict> getAll(Specification<Dict> specification, Page page) {
		org.springframework.data.domain.Page<Dict> springPage = dictDAO.findAll(specification,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		
		return springPage.getContent();
	}

	@Override
	public List<Dict> getDictItem(Long dictId) {
		return dictDAO.findOne(dictId).getChildren();
	}
	
	/**
	 * 判断是否存在字典代码
	 */
	@Override
	public boolean isExistDictCode(String code) {
		List<Dict> dicts = dictDAO.getByCodeAndType(code,Dict.TYPE_DICT);
		if(dicts.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	
	/**
	 * 判断是否存在字典Key
	 */
	@Override
	public boolean isExistItemCode(Long dictId,String itemCode) {
		Dict dict = dictDAO.findOne(dictId);
		return isExistItem(dict.getChildren(),itemCode);
	}
	
	private boolean isExistItem(List<Dict> items,String itemCode){
		for(Dict item : items){
			if(item.getCode().equals(itemCode)){
				return true;
			}
			return isExistItem(item.getChildren(),itemCode);
		}
		return false;
	}
	
	@Override
	public void saveOrUpdateItem(Dict item,Long dictId){
		
		if(isExistItemCode(dictId,item.getCode())==false){
			dictDAO.save(item);
		}else{
			throw new ExistedException("已经存在code为"+ item.getCode()+"的字典项");
		}
	}
	
	
	
	/**
	 *  保存或者更新Dict
	 */
	@Override
	public void saveOrUpdate(Dict dict) {
		
		if(dict.getId()==null){
			if(isExistDictCode(dict.getCode())==false){
				dictDAO.save(dict);
			}else{
				throw new ExistedException("已经存在code为"+ dict.getCode()+"的字典");
			}
			
		}else{
			Dict oldDict = dictDAO.findOne(dict.getId());
			if(oldDict.getCode().equals(dict.getCode())){
				dictDAO.save(dict);
			}else{
				if(isExistDictCode(dict.getCode())==false){
					dictDAO.save(dict);
				}else{
					throw new ExistedException("已经存在code为"+ dict.getCode()+"的字典");
				}
			}
		}
	}
	
	
	
	
	@Override
	public Dict getById(Long id) {
		return dictDAO.findOne(id);
	}
	
	@Override
	public void delete(Dict dict) {
		
		if(dict.getChildren().size()>0){
			for(Dict one : dict.getChildren()){
				this.delete(one);
			}
		}
		dictDAO.delete(dict);
	}
	
	@Override
	public void delete(Long id) {
		dictDAO.delete(id);
	}
	
	@Override
	public List<Dict> treeToList(List<Dict> list) {
		List<Dict> result = new ArrayList<>();
		for(Dict dict : list){
			result.add(dict);
			List<Dict> dictChildren = dict.getChildren();
			if(dict.getChildren().size()>0){
				result.addAll(treeToList(dictChildren));
			}
		}
		
		return result;
	}

	

}
